﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblItemsDAO
    {
        private DBService _db = null;
        public TblItemsDAO(String ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }
        public DataTable TblItem_InsUp(TblItems items, int mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_TblItems_InsUp",
                    new string[] { "@ItemID", "@ItemName", "@ItemNumber", "@Quantity", "@ItemDescription", "@IDCategory", "@BaseSellingPrice", "@TaxPercentageRate", "@PriceTaxCode", "@Description", "@Type", "@ValueOnHand", "@Repeat", "@RollSize" , "@Mode" },
                    new object[] {items.ItemID, items.ItemName, items.ItemNumber, items.Quantity, items.ItemDescription, 
                                    items.IDCategory,  items.BaseSellingPrice, items.TaxPercentageRate, items.PriceTaxCode, items.Description, items.Type, items.ValueOnHand, items.Repeat, items.RollSize, mode},
                    CommandType.StoredProcedure);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetAllProduct()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from vItems", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // get product sell
        public DataTable GetAllProductSell()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from vItems where Type=2", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable GetListItems(int ItemID, string p_ItemName, string CateID, int MethodType, int? startRowIndex, int? maximumRows, string Sort)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_TblItems_Lst",
                    new string[] { "@p_ItemID", "@p_ItemName", "@CateID", "@p_Method_Type", "@RowIndex", "@MaxRows", "@Sort" },
                    new object[] { ItemID, p_ItemName, CateID, 1, startRowIndex, maximumRows, Sort },
                    CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        // delete san pham
        public bool DeleteItem(int ItemID)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("delete from TblItems where ItemID = @ItemID",
                    new string[] { "@ItemID" },
                    new object[] { ItemID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        // update Price

        public bool UpdatePrice(int ItemID, double Price)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("update TblItems set BaseSellingPrice = @Price where ItemID = @ItemID",
                    new string[] { "@ItemID", "@Price" },
                    new object[] { ItemID, Price },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
        // Lấy sản phẩm theo ID
        public TblItems GetItemByID(int ItemID)
        {
            TblItems item = new TblItems();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select vItems.* from vItems where vItems.ItemID = @ItemID",
                    new string[] { "@ItemID" },
                    new object[] { ItemID }
                    , CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    item.ItemID = ItemID;

                    item.ItemName = dr["ItemName"].ToString();
                    item.ItemNumber = dr["ItemNumber"].ToString();
                    item.Quantity = Convert.ToInt32(dr["Quantity"]);
                    item.ItemDescription = dr["ItemDescription"].ToString();
                    item.IDCategory = Convert.ToInt32(dr["IDCategory"]);
                    
                    item.BaseSellingPrice = float.Parse(dr["BaseSellingPrice"].ToString());
                    item.TaxPercentageRate = float.Parse(dr["TaxPercentageRate"].ToString());
                    item.PriceTaxCode = float.Parse(dr["PriceTaxCode"].ToString());
                    item.ImageURL = dr["ImageURL"].ToString();
                    item.Description = dr["Description"].ToString();
                    item.Type = Convert.ToInt32(dr["Type"]);
                    item.ValueOnHand = Convert.ToInt32(dr["ValueOnHand"]);
                    item.Repeat = dr["Repeat"].ToString();
                    item.RollSize = dr["RollSize"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return item;
        }

        // lay san pham theo category top 3

        public DataTable GetProduct_ByCateID(int IDCategory)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select top(3)* from vItems where IDCategory = @IDCategory and Type = 1",
                    new string[] { "@IDCategory" },
                    new object[] { IDCategory },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

       // Load Product By IDCate
        public DataTable GetProductByIDCategory(int IDCategory)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from vItems where IDCategory = @IDCategory",
                    new string[] { "@IDCategory" },
                    new object[] { IDCategory },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        // load product special
        public DataTable GetProduct_Special()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select top(20)* from vItems", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        // update Quantity
        public DataTable UpdateQuanlity(int ItemId, int Quanlity)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("update TblItems set ValueOnHand = @Quanlity where ItemID = @ItemId",
                    new string[] { "@ItemId", "@Quanlity" },
                    new object[] { ItemId, Quanlity }, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
